﻿using Model;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ZBZH_Manage_Order.Models;
using ZBZH_Manage_Order.PublicMethod;

namespace ZBZH_Manage_Order.Controllers.SystemSettings
{
    public class OrganizationRepaymentController : Controller
    {
        ServiceReference1.Service1Client wsdcser = new ServiceReference1.Service1Client();
        ZHHQSERVICE.Service1Client zhhq = new ZHHQSERVICE.Service1Client();
        //
        // GET: /OrganizationSet/

        public ActionResult Index()
        {
            return View();
        }
        #region 获取账户列表
        /// <summary>
        /// 获取商户列表
        /// </summary>
        /// <returns></returns>
        public ActionResult getOrgForm(JqGridParam jqgridparam)
        {
            try
            {
                // 声明实体
                List<Model.View_Order_Organization> List = new List<View_Order_Organization>();
                Dictionary<string, string> Dic = new Dictionary<string, string>();
                // 调用服务接口
                string result = wsdcser.GetOrgListForm(jqgridparam.page, jqgridparam.rows, Dic, GetYZM._YZM, 1);
                // 将服务返回的结果进行优化
                JObject searcho = JObject.Parse(result);
                string bs = searcho["BS"].ToString();
                if (bs == "1")
                {
                    IList<JToken> jieguo = searcho["rows"].Children().ToList();//解析返回的结果
                    List = ParseClass<Model.View_Order_Organization>.JsonList(jieguo);//解析返回的结果\
                    foreach (Model.View_Order_Organization item in List)
                    {
                        item.OrgWaltLastTime = DateTimeHelper.ToDate2(item.OrgWaltLastTime);
                        if (item.OrgWaltLastTime.Length == 14)
                        {
                            item.OrgWaltLastTime = DateTimeHelper.ToDate2(item.OrgWaltLastTime);
                        }
                    }
                    var data = new
                    {
                        total = searcho["total"].ToString(),
                        rows = List
                    };
                    return Json(data);
                }
                else
                {
                    var data = new
                    {
                        total = searcho["total"].ToString(),
                        rows = List
                    };
                    return Json(data);
                }
            }
            catch (Exception ex)
            {
                return Json("操作异常：" + ex + "请联系管理员解决");
            }
        }
        #endregion

        #region 查询账户
        /// <summary>
        /// 
        /// </summary>
        /// <param name="OrgName"></param>
        /// <param name="OrgPerson"></param>
        /// <param name="OrgPhone"></param>
        /// <param name="jqgridparam"></param>
        /// <returns></returns>
        public ActionResult SearchMer(string OrgName, string OrgPerson, string OrgPhone, JqGridParam jqgridparam)
        {
            try
            {
                // 声明实体
                List<Model.Order_Organization_T> List = new List<Order_Organization_T>();
                Dictionary<string, string> Dic = new Dictionary<string, string>();
                Dic.Add("OrgName", OrgName);
                Dic.Add("OrgPerson", OrgPerson);
                Dic.Add("OrgPhone", OrgPhone);
                // 调用服务接口
                string result = wsdcser.GetOrgListForm(jqgridparam.page, jqgridparam.rows, Dic, GetYZM._YZM, 1);
                // 将服务返回的结果进行优化
                JObject searcho = JObject.Parse(result);
                string bs = searcho["BS"].ToString();
                if (bs == "1")
                {
                    IList<JToken> jieguo = searcho["rows"].Children().ToList();//解析返回的结果
                    List = ParseClass<Model.Order_Organization_T>.JsonList(jieguo);//解析返回的结果\
                    var data = new
                    {
                        total = searcho["total"].ToString(),
                        rows = List
                    };
                    return Json(data);
                }
                else
                {
                    var data = new
                    {
                        total = searcho["total"].ToString(),
                        rows = List
                    };
                    return Json(data);
                }
            }
            catch (Exception ex)
            {
                return Json("操作异常：" + ex + "请联系管理员解决");
            }
        }
        #endregion

        #region 查询企业账户未平账流水
        /// <summary>
        /// 查询企业账号流水
        /// Flat:0未平账/1平账/2所有
        /// </summary>
        /// <param name="Mer"></param>
        /// <param name="StartTime"></param>
        /// <param name="EndTime"></param>
        /// <param name="jqgridparam"></param>
        /// <returns></returns>
        public ActionResult Org_Repay(string Mer, int OrgID, string StartTime, string EndTime, JqGridParam jqgridparam)
        {
            try
            {
                StartTime = StartTime.Replace("-", "") + "000000";
                EndTime = EndTime.Replace("-", "") + "235959";
                int Flat = 0;
                Page<View_Order_FlowOrganization> page = new Page<View_Order_FlowOrganization>();//声明实体
                List<Model.View_Order_FlowOrganization> List = new List<Model.View_Order_FlowOrganization>();//声明实体
                //if (PZ != null)
                //{
                //    Flat = int.Parse(PZ);
                //}
                //else
                //{
                //    Flat = 2;
                //}
                string result = wsdcser.GetOrgFlowListFrom(jqgridparam.page, jqgridparam.rows, Mer, OrgID, StartTime, EndTime, Flat, GetYZM._YZM, 1);
                JObject jb = JObject.Parse(result);//对结果序列化
                string bs = jb["BS"].ToString();
                if (bs == "1")
                {
                    IList<JToken> resul = jb["rows"].Children().ToList();//解析返回的结果
                    List = ParseClass<Model.View_Order_FlowOrganization>.JsonList(resul);//解析返回的结果
                    foreach (Model.View_Order_FlowOrganization item in List)
                    {
                        item.OrgFlowTime = DateTimeHelper.ToDate2(item.OrgFlowTime);
                        if (item.OrgFlowTime.Length == 14)
                        {
                            item.OrgFlowTime = DateTimeHelper.ToDate2(item.OrgFlowTime);
                        }
                    }
                    var data = new
                    {
                        total = jb["total"].ToString(),
                        rows = List
                    };
                    return Json(data, JsonRequestBehavior.AllowGet);
                }
                else
                {
                    var data = new
                    {
                        total = jb["total"].ToString(),
                        rows = List
                    };
                    return Json(data);
                }

            }
            catch (Exception ex)
            {
                return Json("操作异常：" + ex + "请联系管理员解决");

            }
        }
        //public ActionResult Org_Repay(string Mer, int OrgID, string Time, JqGridParam jqgridparam)
        //{
        //    try
        //    {
        //        Time = Time.Replace("-", "");
        //        Model.CountCheck List = new CountCheck();
        //        string result = wsdcser.CountCheckOut(jqgridparam.page, jqgridparam.rows, Mer, OrgID, Time, GetYZM._YZM, 1);
        //        List<Model.Order_Organization_T> newList = new List<Order_Organization_T>();
        //        JObject jb = JObject.Parse(result);//对结果序列化
        //        string bs = jb["BS"].ToString();
        //        if (bs == "1")
        //        {
        //            IList<JToken> jieguo = jb["rows"].Children().ToList();//解析返回的结果
        //            IList<JToken> jg1 = jieguo[0].ToList();
        //            IList<JToken> hg2 = jieguo[1].ToList();
        //            Model.CountCheck CountCheck = ParseClass<Model.CountCheck>.JsonDeserialize(jb["rows"].ToString());
        //            string a = CountCheck.TotalFlowNum.ToString();
        //            Session[SessionStorage.Debt] = CountCheck.TotalFlowNum.ToString();



        //            //IList<JToken> jieguo1 = CountCheck.TotalFlowNum;
        //            //newList = ParseClass<Model.Order_Organization_T>.JsonList(jieguo1);
        //           // List = ParseClass<Model.CountCheck>.JsonList(jg1.ToString());//解析返回的结果\
        //            var data = new
        //            {
        //                total = CountCheck.FlowList.Count,
        //                rows =CountCheck.FlowList
                        
        //            };
        //            return Json(data);
        //            //return Content(jb["rows"].ToString());
        //            //IList<JToken> resul = jb["rows"].Children().ToList();//解析返回的结果
        //            //List = ParseClass<Model.Order_Organization_T>.JsonList(resul);//解析返回的结果
        //            //var data = new
        //            //{
        //            //    total = jb["total"].ToString(),
        //            //    rows = List
        //            //};
        //            //return Json(data, JsonRequestBehavior.AllowGet);
        //        }
        //        else
        //        {
        //            return Content(jb["Msg"].ToString());
        //        }

        //    }
        //    catch (Exception ex)
        //    {
        //        return Json("操作异常：" + ex + "请联系管理员解决");

        //    }
        //}
        #endregion

        #region 获取商户欠款
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public ActionResult getDebt()
        {
            try
            {
                string a = Session[SessionStorage.Debt].ToString();
                return Content(Session[SessionStorage.Debt].ToString());
            }
            catch (Exception ex)
            {
                return Content(ex.ToString());
            }
        }
        #endregion

        #region 获取账户的商户集合
        /// <summary>
        /// 
        /// </summary>
        /// <param name="OrgID"></param>
        /// <param name="OrgMers"></param>
        /// <returns></returns>
        public ActionResult ShowMerByID(int OrgID, string OrgMers)
        {
            try
            {
                string[] ArrMer = OrgMers.Split(',');
                Model.View_OperUser user = (Model.View_OperUser)Session[SessionStorage.OperUser];//获取oper

                if (user == null)
                {
                    return RedirectToAction("LoginPage", "Login");  //czx:修改为登录者的Session异常时，直接跳转到登录页面
                }

                List<Model.View_Order_MerSet> List = new List<Model.View_Order_MerSet>();
                string result = wsdcser.GetMer(user.OperID, GetYZM._YZM, 1);
                JObject searcho = JObject.Parse(result);//将服务返回的结果进行优化
                string bs = searcho["BS"].ToString();//获取返回结果是否正常标识
                if (bs == "1")//返回结果正常
                {
                    IList<JToken> jieguo = searcho["rows"].Children().ToList();//解析返回的结果
                    List = ParseClass<Model.View_Order_MerSet>.JsonList(jieguo);//解析返回的结果
                    List<Model.View_Order_MerSet> newList = new List<Model.View_Order_MerSet>();
                    for (int i = 0; i < ArrMer.Length; i++)
                    {
                        foreach (Model.View_Order_MerSet item in List)
                        {
                            //string[] st = item.MerID.Split(',');
                            string tt = ArrMer[i].ToString();
                            if (item.MerID == ArrMer[i].ToString())
                            {
                                newList.Add(item);
                            }
                        }
                    }

                    return Json(newList, JsonRequestBehavior.AllowGet);
                    //return Content(List.TreeToJson());
                }
                else
                {
                    return null;
                }
            }
            catch (Exception ex)
            {
                return Json("操作异常：" + ex + "请联系管理员解决");

            }
        }
        #endregion

        #region 企业平帐
        /// <summary>
        /// 
        /// </summary>
        /// <param name="Data"></param>
        /// <returns></returns>
        public ActionResult repay(string Data)
        {
            try
            {
                //Time = Time.Replace("-", "");
                 string a = Data.Remove(0, 1);
                Model.View_OperUser user = (Model.View_OperUser)Session[SessionStorage.OperUser];

                if (user == null)
                {
                    return RedirectToAction("LoginPage", "Login");  //czx:修改为登录者的Session异常时，直接跳转到登录页面
                }

                string result = wsdcser.FlatOrgFlow(a, user.OperID, GetYZM._YZM, 1);
                JObject jb = JObject.Parse(result);
                string bs = jb["BS"].ToString();
                if (bs == "1")
                {
                    return Content(bs);
                }
                else
                {
                    return Content(bs);
                }
            }
            catch (Exception ex)
            {
                return Json("操作异常：" + ex + "请联系管理员解决");

            }
        }
        #endregion

        #region 空表
        /// <summary>
        /// 
        /// </summary>
        /// <param name="jqgridparam"></param>
        /// <returns></returns>
        public ActionResult repayment(JqGridParam jqgridparam)
        {
            try
            {
                //StartTime = StartTime.Replace("-", "") + "000000";
                //EndTime = EndTime.Replace("-", "") + "235959";
                //int Flat = 0;
                Page<Order_FlowOrganization_T> page = new Page<Order_FlowOrganization_T>();//声明实体
                List<Model.Order_FlowOrganization_T> List = new List<Model.Order_FlowOrganization_T>();//声明实体
                //if (PZ != null)
                //{
                //    Flat = int.Parse(PZ);
                //}
                //else
                //{
                //    Flat = 2;
                //}
                string result = wsdcser.GetOrgFlowListFrom(jqgridparam.page, jqgridparam.rows, "1", 1, "", "19620421210422", 0, GetYZM._YZM, 1);
                JObject jb = JObject.Parse(result);//对结果序列化
                string bs = jb["BS"].ToString();
                if (bs == "1")
                {
                    IList<JToken> resul = jb["rows"].Children().ToList();//解析返回的结果
                    List = ParseClass<Model.Order_FlowOrganization_T>.JsonList(resul);//解析返回的结果
                    var data = new
                    {
                        total = jb["total"].ToString(),
                        rows = List
                    };
                    return Json(data, JsonRequestBehavior.AllowGet);
                }
                else
                {
                    var data = new
                    {
                        total = jb["total"].ToString(),
                        rows = List
                    };
                    return Json(data);
                }

            }
            catch (Exception ex)
            {
                return Json("操作异常：" + ex + "请联系管理员解决");

            }
        }
        //public ActionResult repayment(JqGridParam jqgridparam)
        //{
        //    try
        //    {
                
        //        Model.CountCheck List = new CountCheck();
        //        string result = wsdcser.GetOrgFlowListFrom(jqgridparam.page, jqgridparam.rows, "1", 1, "", "19620421210422", 0, GetYZM._YZM, 1);
        //        List<Model.Order_Organization_T> newList = new List<Order_Organization_T>();
        //        JObject jb = JObject.Parse(result);//对结果序列化
        //        string bs = jb["BS"].ToString();
        //        if (bs == "1")
        //        {
        //            IList<JToken> jieguo = jb["rows"].Children().ToList();//解析返回的结果
        //            IList<JToken> jg1 = jieguo[0].ToList();
        //            IList<JToken> hg2 = jieguo[1].ToList();
        //            Model.CountCheck CountCheck = ParseClass<Model.CountCheck>.JsonDeserialize(jb["rows"].ToString());
        //            Session[SessionStorage.Debt] = CountCheck.TotalFlowNum.ToString();



        //            //IList<JToken> jieguo1 = CountCheck.TotalFlowNum;
        //            //newList = ParseClass<Model.Order_Organization_T>.JsonList(jieguo1);
        //            // List = ParseClass<Model.CountCheck>.JsonList(jg1.ToString());//解析返回的结果\
        //            var data = new
        //            {
        //                total = CountCheck.FlowList.Count,
        //                rows = CountCheck.FlowList

        //            };
        //            return Json(data);
        //            //return Content(jb["rows"].ToString());
        //            //IList<JToken> resul = jb["rows"].Children().ToList();//解析返回的结果
        //            //List = ParseClass<Model.Order_Organization_T>.JsonList(resul);//解析返回的结果
        //            //var data = new
        //            //{
        //            //    total = jb["total"].ToString(),
        //            //    rows = List
        //            //};
        //            //return Json(data, JsonRequestBehavior.AllowGet);
        //        }
        //        else
        //        {
        //            return Content(jb["Msg"].ToString());
        //        }

        //    }
        //    catch (Exception ex)
        //    {
        //        return Json("操作异常：" + ex + "请联系管理员解决");

        //    }
        //}
        #endregion
    }
}